---
title: Add check constraint
description: An add check constraint operation adds a `CHECK` constraint to a column.
---

## Structure

<YamlJsonTabs>
```yaml
alter_column:
  table: table name
  column: column name
  check:
    name: check constraint name
    constraint: constraint expression
  up: SQL expression
  down: SQL expression
```
```json
{
  "alter_column": {
    "table": "table name",
    "column": "column name",
    "check": {
      "name": "check constraint name",
      "constraint": "constraint expression"
    },
    "up": "SQL expression",
    "down": "SQL expression"
  }
}
```
</YamlJsonTabs>

The `up` SQL expression is used to migrate values from the column in the old schema version that aren't subject to the constraint to values in the new schema version that are subject to the constraint.

## Examples

### Add a `CHECK` constraint

Add a `CHECK` constraint to the `title` column in the `posts` table.

The `up` SQL migrates values to ensure they meet the constraint. The `down` SQL copies values without modification from column in the new schema version to the column in the old schema version:

<ExampleSnippet example="22_add_check_constraint.yaml" languange="yaml" />
